using System;
using System.Web.UI.WebControls;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using mojoPortal.Web;
using Resources;
using ud_Article.Business;

namespace ud_Article.UI
{
    public partial class BlogModule : SiteModuleControl
    {

        protected ArticleConfiguration config = new ArticleConfiguration();
        private int countOfDrafts;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += Page_Load;
            EnableViewState = false;
        }

        protected virtual void Page_Load(object sender, EventArgs e)
        {
            LoadSettings();
            SetupCss();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            if (!config.AccordionMode)
            {
                postList.ModuleId = ModuleId;
                postList.PageId = PageId;
                postList.IsEditable = IsEditable;
                postList.Config = config;
                postList.SiteRoot = SiteRoot;
                postList.ImageSiteRoot = ImageSiteRoot;
                AccordionMode.Enabled = false;
                AccordionMode.Visible = false;
            }
            else
            {
                postListAccordion.ModuleId = ModuleId;
                postListAccordion.PageId = PageId;
                postListAccordion.IsEditable = IsEditable;
                postListAccordion.Config = config;
                postListAccordion.SiteRoot = SiteRoot;
                postListAccordion.ImageSiteRoot = ImageSiteRoot;
                NormalMode.Enabled = false;
                NormalMode.Visible = false;
            }
        }

        protected virtual void PopulateLabels()
        {
            Title1.EditUrl = SiteRoot + "/Article/EditPost.aspx";
            Title1.EditText = ArticleResources.BlogAddPostLabel;
            
            if ((IsEditable) && (countOfDrafts > 0))
            {
                //BlogEditCategoriesLabel
                Title1.LiteralExtraMarkup =
                    "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/EditCategory.aspx?pageid=" + PageId.ToInvariantString()
                    + "&amp;mid=" + ModuleId.ToInvariantString()
                    + "' class='editcategorieslink' title='" + ArticleResources.BlogEditCategoriesLabel + "'><img src='/Data/Icon16x16/Folder.png' alt='" + ArticleResources.BlogEditCategoriesLabel + " |'</img></a>"
                    + "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/Drafts.aspx?pageid=" + PageId.ToInvariantString()
                    + "&amp;mid=" + ModuleId.ToInvariantString()
                    + "' class='draftlink' title='" + ArticleResources.BlogDraftsLink + "'><img src='/Data/Icon16x16/Bubble.png' alt='" + ArticleResources.BlogDraftsLink + " |'</img></a>"
                    + "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/ManagePost.aspx?pageid=" + PageId.ToInvariantString()
                    + "&amp;mid=" + ModuleId.ToInvariantString()
                    + "' class='managepostlink' title='" + ArticleResources.AdminManagePostLabel + "'><img src='/Data/Icon16x16/Text.png' alt='" + ArticleResources.AdminManagePostLabel + "'</img></a>";
            }
            else if (IsEditable)
            {
                Title1.LiteralExtraMarkup =
                    "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/EditCategory.aspx?pageid=" + PageId.ToInvariantString()
                    + "&amp;mid=" + ModuleId.ToInvariantString()
                    + "' class='editcategorieslink' title='" + ArticleResources.BlogEditCategoriesLabel + "'><img src='/Data/Icon16x16/Folder.png' alt='" + ArticleResources.BlogEditCategoriesLabel + " |'</img></a>"
                    + "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/ManagePost.aspx?pageid=" + PageId.ToInvariantString()
                    + "&amp;mid=" + ModuleId.ToInvariantString()
                    + "' class='managepostlink' title='" + ArticleResources.AdminManagePostLabel + "'><img src='/Data/Icon16x16/Text.png' alt='" + ArticleResources.AdminManagePostLabel + "'</img></a>";
            }
            else if (WebUser.IsInRoles(config.RolePost) || WebUser.IsInRoles(config.RolePublished))
            {
                Title1.ShowEditLinkOverride = true;
            }
            if (WebUser.IsInRoles(config.RoleProperty))
            {
                Title1.LiteralExtraMarkup +=
                    "&nbsp;<a href='"
                    + SiteRoot
                    + "/Article/EditProperty.aspx?pageid=" + PageId.ToInvariantString()
                    + "&amp;mid=" + ModuleId.ToInvariantString()
                    + "' class='editpropertylink' title='" + ArticleResources.PropertyManage +
                    "'><img src='/Data/Icon16x16/Database.png' alt='" + ArticleResources.PropertyManage +
                    " |'</img></a>";
            }
        }


        protected virtual void LoadSettings()
        {
            pnlContainer.ModuleId = ModuleId;
            config = new ArticleConfiguration(Settings);
            if (config.InstanceCssClass.Length > 0) { pnlContainer.CssClass += " " + config.InstanceCssClass; }

            if (ModuleConfiguration != null)
            {
                Title = ModuleConfiguration.ModuleTitle;
                Description = ModuleConfiguration.FeatureName;
            }


            if (IsEditable)
            {
                countOfDrafts = Article.CountOfDrafts(ModuleId);
            }


        }


        protected virtual void SetupCss()
        {

            // older skins have this
            if (Page.Master == null) return;
            StyleSheet stylesheet = (StyleSheet)Page.Master.FindControl("StyleSheet");
            if (stylesheet == null) return;
            if (stylesheet.FindControl("blogcss") == null)
            {
                Literal cssLink = new Literal
                                      {
                                          ID = "blogcss",
                                          Text = @"<link href='"
                                                 + SiteUtils.GetSkinBaseUrl()
                                                 +
                                                 @"blogmodule.css' type='text/css' rel='stylesheet' media='screen' />"
                                      };

                stylesheet.Controls.Add(cssLink);
            }

            if (stylesheet.FindControl("aspcalendar") == null)
            {
                Literal cssLink = new Literal
                                      {
                                          ID = "aspcalendar",
                                          Text = @"<link href='"
                                                 + SiteUtils.GetSkinBaseUrl()
                                                 +
                                                 @"aspcalendar.css' type='text/css' rel='stylesheet' media='screen' />"
                                      };

                stylesheet.Controls.Add(cssLink);
            }
        }
    }
}
